﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using x1.Framework;
public class UIManager : UnitySingleton<UIManager> {

    public GameObject UIRoot;

    private static GameObject _win;
    private static GameObject _tip;
    private static GameObject _pop;
    private static GameObject _log;

    private Dictionary<string, Dictionary<string, GameObject>> _winViewBuffer = new Dictionary<string, Dictionary<string, GameObject>>();
    //模块缓存
    //private Dictionary<string, Transform> modelDic = new Dictionary<string, Transform>();

    private List<Transform> modelList = new List<Transform>();

    //一个模块下的窗体缓存
    //private Dictionary<string, List<GameObject>> modelWin = new Dictionary<string, List<GameObject>>();
    private Dictionary<string, List<GameObject>> modelWinDic = new Dictionary<string, List<GameObject>>();

    //模块堆栈
    private Stack<GameObject> stackModel = new Stack<GameObject>();
    //窗体堆栈
    private Stack<GameObject> stackWin = new Stack<GameObject>();

    private GameObject _Log;
    public override void Init()
    {
        UIRoot = GameObject.Find("Canvas/UIRoot");
        _win = UIRoot.CreateLocalEmptyChild("Win");
        _tip = UIRoot.CreateLocalEmptyChild("Tip");
        _pop = UIRoot.CreateLocalEmptyChild("Pop");
        _log = UIRoot.CreateLocalEmptyChild("Log");
        _Log = _log.CreateLocalChild(Resources.Load<GameObject>("LogDialog"));
        _Log.SetActive(false);
        _Log.transform.localScale = new Vector2(1.4f, 1.2f);
    }

    void OnGUI()
    {
        //if (!_showLog) return;
        GUI.color = Color.red;
        if (GUI.Button(new Rect(Screen.width - 170, 2, 100, 50), "Log开关"))
        {
            _Log.SetActive(!_Log.activeInHierarchy);
        }
    }

    /// <summary>
    /// 创建UI模块
    /// </summary>
    /// <param name="modelName"></param>
    /// <param name="viewName"></param>
    /// <param name="HideOther"></param>
    /// <returns></returns>
    public GameObject CreateMode(string modelName, string viewName, bool HideOther = true,bool addLuaBehavior = true)
    {
        GameObject view = null;
        Transform model = GetModel(modelName);
        if (model != null)
        {
            //Transform model = modelDic[modelName];
            if(HideOther)
            {
                for (int i = 0; i < modelWinDic[modelName].Count; i++)
                {
                    modelWinDic[modelName][i].SetActive(false);
                }
            }
            view = GetWinViewByViewBuffer(modelName, viewName);            
            if(view == null)
            {
                view = CreateWinView(modelName, viewName, HideOther, addLuaBehavior);
                view.transform.SetParent(model);
                view.transform.localPosition = Vector3.zero;
                view.transform.localScale = Vector3.one;
            }
            else
            {
                view.gameObject.SetActive(true);
            }
        }
        else
        {
            if(HideOther)
            {
                for (int i = 0; i < modelList.Count; i++)
                {
                    modelList[i].gameObject.SetActive(false);
                }
            }
            model = _win.CreateLocalEmptyChild(modelName).transform;
            modelList.Add(model);
            List<GameObject> list = new List<GameObject>();
            modelWinDic.Add(model.name, list);
            view = CreateWinView(modelName,viewName, HideOther, addLuaBehavior);
            view.transform.SetParent(model);
            view.transform.localPosition = Vector3.zero;
            view.transform.localScale = Vector3.one;
        }
        return view;
    }

    /// <summary>
    /// 创建界面
    /// </summary>
    /// <returns></returns>
    public GameObject CreateWinView(string modelName,string viewName,bool HideOther = true,bool addLuaBehavior = true)
    {
        string resName = "Prefabs/" + modelName + "/" + viewName;
        GameObject go = ResManager.Instance.GetRes<GameObject>(resName);
        GameObject view = GameObject.Instantiate(go);
        view.name = go.name;
        //if(addLuaBehavior)
        //    view.AddComponent<FLuaBehaviour>();
        modelWinDic[modelName].Add(view.gameObject);
        return view;
    }

    /// <summary>
    /// 创建Tip界面
    /// </summary>
    /// <returns></returns>
    public GameObject CreateTipView(string modelName, string viewName, bool HideOther = true)
    {
        return null;
    }


    /// <summary>
    /// 创建Pop界面
    /// </summary>
    /// <returns></returns>
    public GameObject CreatePopView(string modelName, string viewName, bool HideOther = true)
    {
        return null;
    }


    /// <summary>
    /// 根据窗体名字获取模块名
    /// </summary>
    /// <param name="winName"></param>
    /// <returns></returns>
    public GameObject GetModelByWinName(string winName)
    {
        return null;
    }

    /// <summary>
    /// 在窗体缓存中获取窗体
    /// </summary>
    /// <param name="viewName"></param>
    /// <returns></returns>
    private GameObject GetWinViewByViewBuffer(string modelName, string viewName)
    {
        List<GameObject> data = modelWinDic[modelName];
        for (int i = 0; i < data.Count; i++)
        {
            if (data[i].name == viewName)
                return data[i];
        }
        return null;
    }


    /// <summary>
    /// 隐藏其他模块
    /// </summary>
    /// <param name="go"></param>
    private void HideOtherModel(GameObject go)
    {
        //foreach (var key in modelDic.Keys)
        //{
        //    if (go.name != key && modelDic[key] != null && modelDic[key].gameObject.activeSelf)
        //    {
        //        modelDic[key].gameObject.SetActive(false);
        //        stackModel.Push(modelDic[key]);
        //    }
        //}
    }

    /// <summary>
    /// 销毁模块
    /// </summary>
    public void DestroyWinModel(string modelName)
    {
        Transform model = GetModel(modelName);
        if (model != null)
        {
            //Transform go = modelDic[modelName];
            modelList.Remove(model);
            Destroy(model.gameObject);
            modelWinDic.Remove(modelName);
            if (modelList.Count > 0)
                modelList[modelList.Count - 1].gameObject.SetActive(true);
        }
    }

    private Transform GetModel(string name)
    {
        for (int i = 0; i < modelList.Count; i++)
        {
            if (modelList[i].name == name)
                return modelList[i];
        }
        return null;
    }
}
